package com.ccc.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HdfsClient {

    Configuration configuration;
    FileSystem fs;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        // 获取文件系统
        configuration = new Configuration();
        fs = FileSystem.get(new URI("hdfs://hadoop102:9820"), configuration, "ccc");
    }

    @After
    public void close() throws IOException {
        // 关闭资源
        fs.close();
    }

    @Test
    public void testMkdirs() throws IOException {
        // 创建文件夹
        fs.mkdirs(new Path("/1001/cc"));
    }

    @Test
    public void testCopyFromLocalFile() throws IOException {
        // 文件上传
        fs.copyFromLocalFile(new Path("C:/Users/83726/Downloads/SQL基础教程 (z-lib.org).pdf"), new Path("/upload/SQL.pdf"));
    }

    @Test
    public void testCopyToLocalFile() throws IOException {
        // 文件下载
        fs.copyToLocalFile(false, new Path("/upload/SQL.pdf"),
                new Path("C:/BigData/SQL.pdf"), true);
    }

    @Test
    public void testDelete() throws IOException {
        // 文件和目录删除
        fs.delete(new Path("/upload/SQL.pdf"), true);
    }

    @Test
    public void testRename() throws IOException {
        fs.rename(new Path("/upload/SQL.pdf"), new Path("/upload/sql.pdf"));
    }

    @Test
    public void testListFiles() throws IOException {
        // 文件详情查看
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus status = listFiles.next();
            System.out.println(status.getPath().getName() + "\t"
                    + status.getLen() / 1024 / 1024.0 + "\t"
                    + status.getPermission() + "\t"
                    + status.getGroup());

            BlockLocation[] blockLocations = status.getBlockLocations();
            for (BlockLocation blockLocation : blockLocations) {
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
            System.out.println("------------------------------");
        }
    }

    @Test
    public void testListStatus() throws IOException {
        // 文件夹和文件判断
        FileStatus[] listStatus = fs.listStatus(new Path("/upload"));
        for (FileStatus status : listStatus) {
            if (status.isFile())
                System.out.println("file: " + status.getPath().getName());
            else
                System.out.println("dir: " + status.getPath().getName());
        }
    }

}
